<?php

namespace App\Console\Commands;

use App\Dep\AccountDep;
use App\Dep\ContentDep;
use App\Dep\ContentLogDep;
use App\Enum\CommonEnum;
use App\Enum\ContentEnum;
use App\Lib\JdWebSdk;
use Illuminate\Console\Command;

class SyncStatusCmd extends Command
{
    public $description = '同步发布之后的状态';
    public $signature = 'content:sync-status';

    public function handle()
    {
        $this->log('content sync begin');
        $this->syncJingdongweb();

    }

    private function log($msg, $context = [])
    {
        $this->info("sync log: {$msg}");
        log_daily("content_sync_status")->info($msg, $context);
    }

    public function syncJingdongweb()
    {
        $this->log('begin jingdong web');
        $contentDep = new ContentDep();
        $resList = $contentDep->getReleased(ContentEnum::CHANNEL_VIDEO_JINGDONGGUANG_WEB);
        if ($resList->isEmpty()) {
            $this->log('jingdong web,has no task');
            return false;
        }

        $resList->each(function ($item) {
            sleep(1);
            $this->log("deal {$item->id}");

            if(!$item->platform_id){
                $this->log('未查询到平台内容id');
                return true;
            }

            $accountDep = new AccountDep();
            $resAccount = $accountDep->first($item->account_id);
            if (!$resAccount) {
                $this->log('未查询到发布账号');
                return true;
            }

            if ($resAccount->is_del == CommonEnum::YES) {
                $this->log('发布账号已被删除');
                return true;
            }

            if (!$resAccount->cookie) {
                $this->log('未查询到账号cookie');
                return true;
            }

            $service = new JdWebSdk($resAccount->cookie);
            $resStatus = $service->syncStatus($item->platform_id);
            if (!isset($resStatus['code']) || $resStatus['code'] != 0) {
                $this->log('同步报错:' . json_encode($resStatus));
                return true;
            }

            if (empty($resStatus['result']['contentList'])) {
                $this->log('未查询到内容');
                return true;
            }

            $content = $resStatus['result']['contentList'][0];

            //私域上线
            if ($content['contentStatus'] == 2 && !empty($content['offerReason'])) {
                $this->taskError($item->id, $content['offerReason']);;
                $this->log("done {$item->id}");
                return true;
            }

            //公域上线
            if ($content['contentStatus'] == 3) {
                $this->taskSuccess($item->id);;
                $this->log("done {$item->id}");
                return true;
            }

            //审核驳回
            if ($content['contentStatus'] == 5) {
                $this->taskError($item->id, $content['offerReason']);;
                $this->log("done {$item->id}");
                return true;
            }
            //视频转码失败
            if ($content['contentStatus'] == 6) {
                $this->taskError($item->id, '视频转码失败');;
                $this->log("done {$item->id}");
                return true;
            }
//            sleep(5);
        });
    }


    private function taskError($contentId, $reason)
    {

        $contentDep = new ContentDep();
        $contentData = [
            'status' => ContentEnum::STATUS_RELEASE_ADJUST,  //内部修改线上
            'latest_log' => $reason
        ];

        $contentLogDep = new ContentLogDep();
        $logData = [
            'content_id' => $contentId,
            'description' => "系统自动同步状态-审核未通过",
        ];
        $contentLogDep->add($logData);

        $contentDep->edit($contentId, $contentData);
    }

    private function taskSuccess($contentId)
    {

        $contentDep = new ContentDep();
        $contentData = [
            'status' => ContentEnum::STATUS_RELEASE_APPROVED,  //内部修改线上
            'latest_log' => ""
        ];

        $contentDep->edit($contentId, $contentData);

        $contentLogDep = new ContentLogDep();
        $logData = [
            'content_id' => $contentId,
            'description' => "系统自动同步状态-审核通过",
        ];
        $contentLogDep->add($logData);
    }
}
